﻿Imports MyDLL
Imports LinqXmlData

Public Class Form1

    Dim Document As New MyDLL.Document
    Dim SaveOpen As New MyDLL.SaveOpen
    Dim Types As New LinqXmlData.Types

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dylan_s_XML_Office_Database.Form2.Show()
        OptLoad()
        OptApply()
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox2.SelectedItem = ComboBox1.SelectedItem
        Form10.ComboBox1.SelectedItem = ComboBox1.SelectedItem
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        ComboBox1.SelectedItem = ComboBox2.SelectedItem
    End Sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        TextBox5.Text = TextBox2.Text
        Form10.TextBox2.Text = TextBox2.Text
    End Sub
    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        TextBox2.Text = TextBox5.Text
    End Sub
    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        TextBox6.Text = TextBox3.Text
        Form10.TextBox3.Text = TextBox3.Text
    End Sub

    Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
        TextBox3.Text = TextBox6.Text
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox4.Text = TextBox1.Text
        Form10.TextBox1.Text = TextBox1.Text
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        TextBox1.Text = TextBox4.Text
    End Sub


    Sub Save()

        If CheckBox7.Checked Then
            SaveOpen.FormAdd = False
        Else
            SaveOpen.FormAdd = True
        End If

        If CheckBox1.Checked Then
            'Code for Saving File -- Customize names as needed

            ActionLabel.Text = "Saving File"

            SaveOpen.SaveXml(ComboBox1, "Drive", TextBox7, TextBox1.Text, TextBox2.Text, "Desktop", _
                             "My Documents", TextBox3.Text, RichTextBox1)

            Me.Text = "Dylan's XML Office Database" & " - " & TextBox3.Text

            If CheckBox8.Checked Then
                If CheckBox1.Checked Then
                    Try
                        Dim Data As XDocument = XDocument.Load(TextBox7.Text & TextBox3.Text & ".xml")

                        My.Computer.FileSystem.WriteAllText(TextBox7.Text & TextBox3.Text & ".xml", Data.ToString, False)

                    Catch ex As Exception
                        MsgBox("Document Formatting Failed!", , "Document Formatting")
                    End Try

                End If
            End If

        End If

        If CheckBox2.Checked Then
            'Code for Saving File -- Customize names as needed

            ActionLabel.Text = "Saving File"

            SaveOpen.SaveXaml(ComboBox1, "Drive", TextBox7, TextBox1.Text, TextBox2.Text, "Desktop", _
                             "My Documents", TextBox3.Text, RichTextBox2)

            Me.Text = "Dylan's XML Office Database" & " - " & TextBox3.Text

        End If

        ActionLabel.Text = "Ready"

    End Sub

    Sub Open()

        If CheckBox7.Checked Then
            SaveOpen.FormAdd = False
        Else
            SaveOpen.FormAdd = True
        End If

        If CheckBox1.Checked Then
            'Code for Saving File -- Customize names as needed

            ActionLabel.Text = "Opening File"

            SaveOpen.OpenXml(ComboBox1, "Drive", TextBox7, TextBox1.Text, TextBox2.Text, _
                             "Desktop", "My Documents", TextBox3.Text, RichTextBox1)

            Me.Text = "Dylan's XML Office Database" & " - " & TextBox3.Text
        End If

        ActionLabel.Text = "Ready"

        If CheckBox2.Checked Then
            'Code for Saving File -- Customize names as needed

            ActionLabel.Text = "Opening File"

            SaveOpen.OpenXaml(ComboBox1, "Drive", TextBox7, TextBox1.Text, TextBox2.Text, _
                              "Desktop", "My Documents", TextBox3.Text, RichTextBox2)


            Me.Text = "Dylan's XML Office Database" & " - " & TextBox3.Text

        End If
        FormatDoc()
        ActionLabel.Text = "Ready"
    End Sub



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form3.Show()
        OptLoad()
        OptApply()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form4.Show()
        OptLoad()
        OptApply()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Form5.Show()
        OptLoad()
        OptApply()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Form6.Show()
        OptLoad()
        OptApply()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Form7.Show()
        OptLoad()
        OptApply()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Open()
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Save()
    End Sub


    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub


    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        AboutBox1.Show()
    End Sub


    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim XmlSO As String = CheckBox3.CheckState.ToString
        Dim XamlSO As String = CheckBox4.CheckState.ToString
        Dim XmlF As String = CheckBox5.CheckState.ToString
        Dim XamlF As String = CheckBox6.CheckState.ToString
        Dim SMTP As String = TextBox8.Text
        Dim Email As String = TextBox9.Text
        Dim Name As String = TextBox10.Text
        Dim Formatting As String = CheckBox8.CheckState.ToString
        Dim SearchAdd As String = TextBox13.Text

        Dim Options As XElement = _
        <Options>
            <SO>
                <XML><%= XmlSO %></XML>
                <XAML><%= XamlSO %></XAML>
                <Formatting><%= Formatting %></Formatting>
            </SO>
            <F>
                <XML><%= XmlF %></XML>
                <XAML><%= XamlF %></XAML>
            </F>
            <Email>
                <SMTP><%= SMTP %></SMTP>
                <Email><%= Email %></Email>
                <Name><%= Name %></Name>
            </Email>
            <Search>
                <Location><%= SearchAdd %></Location>
            </Search>
        </Options>

        If My.Computer.FileSystem.DirectoryExists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO") = False Then
            My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO")
        End If

        My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\XMLDB.xml", _
                            Options.ToString, False)

    End Sub
    Private Sub OptLoad()
        Try
            Dim Options As XElement = XElement.Load(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\XMLO\XMLDB.xml")

            Dim XmlSO As XElement = _
                   <Root>
                       <%= From el In Options.<SO>.<XML> _
                           Select el %>
                   </Root>

            If XmlSO = "Checked" Then
                CheckBox3.CheckState = CheckState.Checked
            End If

            If XmlSO = "Unchecked" Then
                CheckBox3.CheckState = CheckState.Unchecked
            End If

            Dim XamlSO As XElement = _
                   <Root>
                       <%= From el In Options.<SO>.<XAML> _
                           Select el %>
                   </Root>

            If XamlSO = "Checked" Then
                CheckBox4.CheckState = CheckState.Checked
            End If

            If XamlSO = "Unchecked" Then
                CheckBox4.CheckState = CheckState.Unchecked
            End If

            Dim Formatting As XElement = _
                 <Root>
                     <%= From el In Options.<SO>.<Formatting> _
                         Select el %>
                 </Root>

            If Formatting = "Checked" Then
                CheckBox8.CheckState = CheckState.Checked
            End If

            If Formatting = "Unchecked" Then
                CheckBox8.CheckState = CheckState.Unchecked
            End If

            Dim XmlF As XElement = _
                   <Root>
                       <%= From el In Options.<F>.<XML> _
                           Select el %>
                   </Root>

            If XmlF = "Checked" Then
                CheckBox5.CheckState = CheckState.Checked
            End If

            If XmlF = "Unchecked" Then
                CheckBox5.CheckState = CheckState.Unchecked
            End If

            Dim XamlF As XElement = _
                   <Root>
                       <%= From el In Options.<F>.<XAML> _
                           Select el %>
                   </Root>

            If XamlF = "Checked" Then
                CheckBox6.CheckState = CheckState.Checked
            End If

            If XamlF = "Unchecked" Then
                CheckBox6.CheckState = CheckState.Unchecked
            End If

            Dim SMTP As XElement = _
                   <Root>
                       <%= From el In Options.<Email>.<SMTP> _
                           Select el %>
                   </Root>
            TextBox8.Text = SMTP

            Dim Email As XElement = _
                   <Root>
                       <%= From el In Options.<Email>.<Email> _
                           Select el %>
                   </Root>
            TextBox9.Text = Email

            Dim Name As XElement = _
                   <Root>
                       <%= From el In Options.<Email>.<Name> _
                           Select el %>
                   </Root>
            TextBox10.Text = Name

            Dim SearchAdd As XElement = _
            <Root>
                <%= From el In Options.<Search>.<Location> _
                    Select el %>
            </Root>
            TextBox13.Text = SearchAdd

        Catch ex As Exception
            MsgBox("Please save the file!")
        End Try
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        OptLoad()
    End Sub

    Private Sub OptApply()
        Try
            If CheckBox3.CheckState = CheckState.Checked Then
                CheckBox1.CheckState = CheckState.Checked
            End If
            If CheckBox3.CheckState = CheckState.Unchecked Then
                CheckBox1.CheckState = CheckState.Unchecked
            End If

            If CheckBox4.CheckState = CheckState.Checked Then
                CheckBox2.CheckState = CheckState.Checked
            End If
            If CheckBox4.CheckState = CheckState.Unchecked Then
                CheckBox2.CheckState = CheckState.Unchecked
            End If

            If CheckBox5.CheckState = CheckState.Checked Then
                Form2.CheckBox1.CheckState = CheckState.Checked
                Form3.CheckBox1.CheckState = CheckState.Checked
                Form4.CheckBox1.CheckState = CheckState.Checked
                Form5.CheckBox1.CheckState = CheckState.Checked
                Form6.CheckBox1.CheckState = CheckState.Checked
                Form7.CheckBox1.CheckState = CheckState.Checked
                Form9.CheckBox1.CheckState = CheckState.Checked
                Form11.CheckBox1.CheckState = CheckState.Checked
                Form16.CheckBox1.CheckState = CheckState.Checked
            End If

            If CheckBox5.CheckState = CheckState.Unchecked Then
                Form2.CheckBox1.CheckState = CheckState.Unchecked
                Form3.CheckBox1.CheckState = CheckState.Unchecked
                Form4.CheckBox1.CheckState = CheckState.Unchecked
                Form5.CheckBox1.CheckState = CheckState.Unchecked
                Form6.CheckBox1.CheckState = CheckState.Unchecked
                Form7.CheckBox1.CheckState = CheckState.Unchecked
                Form9.CheckBox1.CheckState = CheckState.Unchecked
                Form11.CheckBox1.CheckState = CheckState.Unchecked
                Form16.CheckBox1.CheckState = CheckState.Unchecked
            End If


            If CheckBox6.CheckState = CheckState.Checked Then
                Form2.CheckBox2.CheckState = CheckState.Checked
                Form3.CheckBox2.CheckState = CheckState.Checked
                Form4.CheckBox2.CheckState = CheckState.Checked
                Form5.CheckBox2.CheckState = CheckState.Checked
                Form6.CheckBox2.CheckState = CheckState.Checked
                Form7.CheckBox2.CheckState = CheckState.Checked
                Form9.CheckBox2.CheckState = CheckState.Checked
                Form11.CheckBox2.CheckState = CheckState.Checked
                Form16.CheckBox2.CheckState = CheckState.Checked
            End If

            If CheckBox6.CheckState = CheckState.Unchecked Then
                Form2.CheckBox2.CheckState = CheckState.Unchecked
                Form3.CheckBox2.CheckState = CheckState.Unchecked
                Form4.CheckBox2.CheckState = CheckState.Unchecked
                Form5.CheckBox2.CheckState = CheckState.Unchecked
                Form6.CheckBox2.CheckState = CheckState.Unchecked
                Form7.CheckBox2.CheckState = CheckState.Unchecked
                Form9.CheckBox2.CheckState = CheckState.Unchecked
                Form11.CheckBox2.CheckState = CheckState.Unchecked
                Form16.CheckBox2.CheckState = CheckState.Unchecked
            End If
        Catch ex As Exception
            MsgBox("Apply failed!")
        End Try
    End Sub
    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        OptApply()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ActionLabel.Text = "Program Loaded"
        TabControl1.SelectTab(1)
        OptLoad()
        OptApply()
    End Sub

    Private Sub EmailToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmailToolStripMenuItem.Click
        Form8.Show()
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Form9.Show()
        OptLoad()
        OptApply()
    End Sub

    Private Sub FormatDoc()
        If CheckBox8.Checked Then
            If CheckBox1.Checked Then
                Document.FormatDoc(TextBox7.Text & TextBox3.Text & ".xml", RichTextBox1)
            End If
        End If
    End Sub

    Private Sub TextEditorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextEditorToolStripMenuItem.Click
        Form10.Show()
    End Sub

    Private Sub DataEditorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataEditorToolStripMenuItem.Click
        Try
            Dim File As XElement = XElement.Load(Me.TextBox7.Text & Me.TextBox3.Text & ".xml")

            Dim Type As XElement = _
            <Node>
                <%= From el In File.<Type> _
                    Select el %>
            </Node>

            If Type = Types.Contacts Then
                Form2.Show()
                OptLoad()
                OptApply()

            ElseIf Type = Types.Passwords Then
                Form3.Show()
                OptLoad()
                OptApply()

            ElseIf Type = Types.Timetables Then
                Form4.Show()
                OptLoad()
                OptApply()

            ElseIf Type = Types.Favourites Then
                Form5.Show()
                OptLoad()
                OptApply()

            ElseIf Type = Types.Petrol Then
                Form6.Show()
                OptLoad()
                OptApply()

            ElseIf Type = Types.Stocks Then
                Form7.Show()
                OptLoad()
                OptApply()

            ElseIf Type = Types.Accounts Then
                Form9.Show()
                OptLoad()
                OptApply()

            ElseIf Type = Types.Appointments Then
                Form11.Show()
                OptLoad()
                OptApply()

            ElseIf Type = Types.Documents Then
                Form16.Show()
                OptLoad()
                OptApply()

            End If

        Catch ex As Exception
            MsgBox("Please Load a Valid Document")
        End Try
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Form11.Show()
        OptLoad()
        OptApply()
    End Sub

    Private Sub TableXMLMakerReaderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TableXMLMakerReaderToolStripMenuItem.Click
        Form13.Show()
    End Sub


    Private Sub BrowserToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowserToolStripMenuItem.Click
        Form15.Show()
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Form16.Show()
        OptLoad()
        OptApply()
    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        RichTextBox1.Clear()
        RichTextBox2.Clear()
    End Sub

    Private Sub SearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchToolStripMenuItem.Click
        Form18.Show()
        OptLoad()
        OptApply()
    End Sub
End Class